package com.zhangh.summer.admin.biz.role.impl;

import com.zhangh.summer.admin.model.bo.DataBO;
import com.zhangh.summer.admin.model.po.AdRolePO;
import com.zhangh.summer.admin.biz.role.RoleBiz;
import com.zhangh.summer.admin.model.bo.NoValidatorBO;
import com.zhangh.summer.admin.model.po.AdRolePOExample;
import com.zhangh.summer.admin.model.po.AdUserRolePOExample;
import com.zhangh.summer.admin.support.aop.tx.MultiDs;
import com.zhangh.summer.admin.support.ds.common.MixedDS;
import com.zhangh.summer.admin.support.ds.common.pager.DataPager;
import com.zhangh.summer.admin.support.ds.common.pager.Pager;
import com.zhangh.summer.admin.support.exception.BizException;
import com.zhangh.summer.admin.support.id.IdGen;
import com.zhangh.summer.admin.support.session.SysSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2021-09-28 01:35
 **/
@Service
@Slf4j
public class RoleBizImpl implements RoleBiz {
    @Resource
    private MixedDS mixedDS;
    @Autowired
    private IdGen idGen;

    @Override
    @MultiDs
    public DataPager<AdRolePO> getRoleDataPager(DataBO query, Pager pager) {
        AdRolePOExample example=new AdRolePOExample();
        AdRolePOExample.Criteria c=example.createCriteria();
        if (StringUtils.isNotEmpty(query.getName())){
            c.andRoleNameLike("%"+query.getName()+"%");
            example.or().andRoleNoEqualTo(query.getName());
        }
        DataPager<AdRolePO> dataPager=this.mixedDS.txDs().mapper().selectDataPager(example,pager);
        return dataPager;
    }

    @Override
    @MultiDs
    public AdRolePO getRolePO(Long id) {
        AdRolePO adRolePO=this.mixedDS.txDs().mapper().selectByPrimaryKey(AdRolePO.class,id);
        return adRolePO;
    }

    @Override
    @MultiDs
    @Transactional
    public long saveRole(AdRolePO adRolePO) {
        adRolePO.setId(idGen.getID());
        adRolePO.setCreateBy(SysSession.getUser().getLoginName());
        adRolePO.setCreateTime(new Date());
        adRolePO.setUpdateBy(SysSession.getUser().getLoginName());
        adRolePO.setUpdateTime(new Date());
        int c1=this.mixedDS.txDs().mapper().insert(adRolePO);
        log.info("保存角色 id {}",adRolePO.getId());
        return adRolePO.getId();
    }

    @Override
    @MultiDs
    @Transactional
    public int updateRole(AdRolePO adRolePO) {
        adRolePO.setUpdateBy(SysSession.getUser().getLoginName());
        adRolePO.setUpdateTime(new Date());
        int c1=this.mixedDS.txDs().mapper().updateByPrimaryKeySelective(adRolePO);
        log.info("更新角色 id {}",adRolePO.getId());
        return c1;
    }

    @Override
    @MultiDs
    @Transactional
    public int deleteRole(long id) {
        AdUserRolePOExample adUserRolePOExample=new AdUserRolePOExample();
        adUserRolePOExample.createCriteria().andRoleIDEqualTo(id);
        long c1=this.mixedDS.txDs().mapper().countByExample(adUserRolePOExample);
        if (c1>0){
            log.error("角色id {}已经被使用，不可删除",id);
            throw BizException.exception("角色id {0}已经被使用，不可删除",id);
        }
        int c2=this.mixedDS.txDs().mapper().deleteByPrimaryKey(AdRolePO.class,id);
        log.info("删除角色 id {}",id);
        return c2;
    }

    @Override
    public boolean validateRoleNo(NoValidatorBO noValidatorBO) {
        AdRolePOExample example=new AdRolePOExample();
        if (StringUtils.isNotBlank(noValidatorBO.getOriginalNo())){
           if (noValidatorBO.getNewNo().equals(noValidatorBO.getOriginalNo())){
               return true;
           }
        }
        example.createCriteria().andRoleNoEqualTo(noValidatorBO.getNewNo());
        long c1=this.mixedDS.txDs().mapper().countByExample(example);
        if (c1==0){
            return true;
        }
        return false;
    }

    @Override
    public List<AdRolePO> getRoleDataList(DataBO query) {
        AdRolePOExample example=new AdRolePOExample();
        AdRolePOExample.Criteria c=example.createCriteria();
        if (StringUtils.isNotEmpty(query.getName())){
            c.andRoleNameLike("%"+query.getName()+"%");
            example.or().andRoleNoEqualTo(query.getName());
        }
        List<AdRolePO> list=this.mixedDS.txDs().mapper().selectByExample(example);
        return list;
    }

    @Override
    public List<AdRolePO> getUserRole(DataBO query) {
        Map<String,Object> params=new HashMap<>();
        params.put("userID",query.getId());
        List<AdRolePO> list=this.mixedDS.txDs().mapper("RoleMapper").selectList("selectUserRole",params);
        return list;
    }
}
